package com.hxh.wechat.activity

import android.content.Context
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.JsonObject
import com.hxh.wechat.R
import com.hxh.wechat.api.UserApi
import com.hxh.wechat.constant.GlobalConstant
import com.hxh.wechat.database.UserDbHelper
import com.hxh.wechat.model.ResultStatus
import com.hxh.wechat.util.DBUtil
import com.hxh.wechat.util.getDeviceId
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException


class Login : BaseActivity() {
    val handler = Handler()
    var _this = this
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login)

        val login = isLogin(_this)

        if (login) {
            //直接登录
            val intent = Intent(this, Router::class.java)
            startActivity(intent)
        }

        val loginButton = findViewById<Button>(R.id.login)
        val textViewRegister = findViewById<TextView>(R.id.textViewRegister)

        textViewRegister.setOnClickListener {
            val intent = Intent(this, RegisterActivity::class.java)
            startActivity(intent)
        }

        loginButton.setOnClickListener {
            // 处理登录结果
            val username = findViewById<EditText>(R.id.username).text.toString()
            val password = findViewById<EditText>(R.id.password).text.toString()
            val intent = Intent(this@Login, Router::class.java)
            startActivity(intent)
            finish()
            return@setOnClickListener;
            println(findViewById<CheckBox>(R.id.checkBoxLogin).isChecked);
            println("==============");
            UserApi.login(username, password, getDeviceId(this))?.enqueue(
                object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        println("Failed.")
                        println(e.stackTrace.toString())

                    }

                    @Throws(IOException::class)
                    override fun onResponse(call: Call, response: Response) {
                        val responseData = response.body?.string()
                        val statusString = DBUtil.parseStatusString(responseData!!)
                        if (statusString == ResultStatus.ERROR.code) {
                            handler.post {
                                DBUtil.popMsg(
                                    DBUtil.getGson().fromJson(responseData, JsonObject::class.java)
                                        .get("data").asString, _this
                                );
                            }
                        } else if (statusString == ResultStatus.OK.code) {
                            //登录成功 判断是否有没有box激活
                            val userInfo = DBUtil.parseObjString(responseData.toString());
                            if (findViewById<CheckBox>(R.id.checkBoxLogin).isChecked) {
                                userInfo!!.addProperty("auto", true)
                            }
                            DBUtil.setData(_this, GlobalConstant.USER_INFO, userInfo.toString());
                            // 处理响应数据
                            println(responseData)
                            // 登录成功，跳转到主界面
                            handler.post {
                                val intent = Intent(this@Login, Router::class.java)
                                startActivity(intent)
                                finish()
                            }
                        }
                    }
                })
        }

    }

    fun isLogin(context: Context): Boolean {
        try {
            var user = DBUtil.getUser(context)
            if (user == null || !user.auto) {
                return false
            }
            return true
        } catch (e: Exception) {
            return false
        }
    }


}